package com.hellobike.haxing.modules.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.modules.purchase.entity.TPurchaseOrder;
import com.hellobike.haxing.modules.purchase.entity.TPurchaseOrderDetail;
import com.hellobike.haxing.modules.purchase.enums.OrderState;
import com.hellobike.haxing.modules.purchase.mapper.TPurchaseOrderDetailMapper;
import com.hellobike.haxing.modules.purchase.mapper.TPurchaseOrderMapper;
import com.hellobike.haxing.modules.purchase.service.ITPurchaseOrderDetailService;
import com.hellobike.haxing.modules.supplier.util.BeanUtils;
import com.hellobike.haxing.modules.supplier.util.Convert;
import com.hellobike.haxing.modules.wms.entity.Warehouse;
import com.hellobike.haxing.modules.wms.mapper.WarehouseMapper;
import com.hellobike.haxing.modules.wms.utils.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: t_purchase_order_detail
 * @Author: haxing-boot
 * @Date:   2020-02-12
 * @Version: V1.0
 */
@Service
public class TPurchaseOrderDetailServiceImpl extends ServiceImpl<TPurchaseOrderDetailMapper, TPurchaseOrderDetail> implements ITPurchaseOrderDetailService {

    @Autowired
    private TPurchaseOrderDetailMapper tPurchaseOrderDetailMapper;

    @Autowired
    private TPurchaseOrderMapper tPurchaseOrderMapper;

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public void editSupplier4NewOrder(TPurchaseOrderDetail tPurchaseOrderDetail) {

        // 如果供应商相同就不更新
        TPurchaseOrderDetail orderDetail = tPurchaseOrderDetailMapper.selectById(tPurchaseOrderDetail.getId());
        if(orderDetail.getSupplierId().equals(tPurchaseOrderDetail.getSupplierId())){
            return;
        }

        // 创建新主订单并将订单详情关联上
        QueryWrapper<TPurchaseOrder> orderQuery = new QueryWrapper<>();
        orderQuery.eq("order_code", orderDetail.getParentOrderCode());
        orderQuery.last("limit 1");
        TPurchaseOrder tPurchaseOrder = tPurchaseOrderMapper.selectOne(orderQuery);

        TPurchaseOrder inOrderDo = new TPurchaseOrder();
        BeanUtils.copyProperties(tPurchaseOrder,inOrderDo);
        inOrderDo.setSupplierId(tPurchaseOrderDetail.getSupplierId());
        inOrderDo.setSupplierName(tPurchaseOrderDetail.getSupplierName());
        if(tPurchaseOrderDetail.getWareHouseIds() != null) {
            inOrderDo.setWarehouseId(Long.valueOf(tPurchaseOrderDetail.getWareHouseIds()));
        }
        inOrderDo.setOrderCode(Convert.getOrderCodeByTime());
        inOrderDo.setCreateTime(new Date());
        inOrderDo.setUpdateTime(new Date());
        if(tPurchaseOrder.getWarehouseId() != null) {
            Warehouse warehouse = warehouseMapper.selectById(tPurchaseOrder.getWarehouseId());
            if(warehouse != null){
                inOrderDo.setWarehouseName(warehouse.getWarehouseName());
            }
        }
        inOrderDo.setState(OrderState.ORDER_STATE_0.getIndex());

        TPurchaseOrderDetail temp = tPurchaseOrderDetailMapper.selectById(tPurchaseOrderDetail.getId());
        if(temp.getPurchaseVolume() != null && temp.getPurchasePrice() != null && tPurchaseOrder.getPurchasePrice() != null) {
            inOrderDo.setPurchasePrice(temp.getPurchasePrice().multiply(new BigDecimal(temp.getPurchaseVolume())));
        }
        tPurchaseOrderMapper.insert(inOrderDo);

        // 更新供应商
        tPurchaseOrderDetail.setParentOrderCode(inOrderDo.getOrderCode());
        tPurchaseOrderDetailMapper.updateById(tPurchaseOrderDetail);

        // 如果这个采购单没有其他子单 则取消
        QueryWrapper<TPurchaseOrderDetail> orderDetailQuery = new QueryWrapper<>();
        orderDetailQuery.eq("parent_order_code", tPurchaseOrder.getOrderCode());
        List<TPurchaseOrderDetail> tPurchaseOrderDetails = tPurchaseOrderDetailMapper.selectList(orderDetailQuery);
        if(ListUtil.isNullOrEmpty(tPurchaseOrderDetails)){
            TPurchaseOrder updateDo = new TPurchaseOrder();
            updateDo.setId(tPurchaseOrder.getId());
            updateDo.setState(OrderState.ORDER_STATE_2.getIndex());
            tPurchaseOrderMapper.updateById(updateDo);
        }else { // 如果还存在 则减去总订单的数量
            if(temp.getPurchaseVolume() != null && temp.getPurchasePrice() != null && tPurchaseOrder.getPurchasePrice() != null) {
                TPurchaseOrder updateDo = new TPurchaseOrder();
                updateDo.setId(tPurchaseOrder.getId());
                BigDecimal subPrice = temp.getPurchasePrice().multiply(new BigDecimal(temp.getPurchaseVolume()));
                updateDo.setPurchasePrice(tPurchaseOrder.getPurchasePrice().subtract(subPrice));
                tPurchaseOrderMapper.updateById(updateDo);
            }
        }


    }

    @Override
    public void updateBySkuIdAndSupplier(Long skuId,Integer supplyVolume){
        tPurchaseOrderDetailMapper.updateBySkuIdAndSupplier(skuId,supplyVolume);
    }
}
